//
//  UIImage+DDAdd.h
//  Pods
//
//  Created by 许龙 on 2019/9/26.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIImage (DDAdd)

#pragma mark - Create image

/**
 Create and return a 1x1 point size image with the given color.
 
 @param color  The color.
 */
+ (nullable UIImage *)imageWithColor:(UIColor *)color;

/**
 Create and return a pure color image with the given color and size.
 
 @param color  The color.
 @param size   New image's type.
 */
+ (nullable UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size;

/**
Create and return a pure color image with the given color and size.
 
@param name   The name of image
@param color  The background color.
@param size   New image's type.
*/
+ (nullable UIImage *)imageNamed:(NSString *)name backgroundColor:(UIColor *)color size:(CGSize)size;

/**
 Create and return an image with custom draw code.
 
 @param size      The image size.
 @param drawBlock The draw block.
 
 @return The new image.
 */
+ (nullable UIImage *)imageWithSize:(CGSize)size drawBlock:(void (^)(CGContextRef context))drawBlock;

/**
 Create and return an image with the ttf font.
 
 @param unicode  The unicode of font
 @param fontName The name of font
 @param fontSize The size of font
 @param color    The color of image
 
 @return The new image.
 */
+ (nullable UIImage *)imageWithUnicode:(NSString *)unicode
                              fontName:(NSString *)fontName
                              fontSize:(CGFloat)fontSize
                                 color:(UIColor *)color;

/**
Create and return an image with the "QRCode" string.

@param qrcode  Qrcode string
@param size    The size of image

@return The new image.
*/
+ (nullable UIImage *)imageWithQRCode:(NSString *)qrcode size:(CGFloat)size;

#pragma mark - Image Info

/**
 Whether this image has alpha channel.
*/
- (BOOL)hasAlphaChannel;

#pragma mark - Modify Image

/**
Returns a new rotated image (relative to the center).

@param angle     Rotated angle in counterclockwise.⟲

@param fitSize   YES: new image's size is extend to fit all content.
                 NO: image's size will not change, content may be clipped.
*/
- (nullable UIImage *)imageByAngle:(CGFloat)angle fitSize:(BOOL)fitSize;

/**
 Returns a new rotated image (relative to the center).
 
 @param radians   Rotated radians in counterclockwise.⟲
 
 @param fitSize   YES: new image's size is extend to fit all content.
                  NO: image's size will not change, content may be clipped.
 */
- (nullable UIImage *)imageByRotate:(CGFloat)radians fitSize:(BOOL)fitSize;

/**
 Returns a new fixed orientation error image.
 After take photo should call the method
*/
- (nullable UIImage *)imageByFixedTakePhotoOrientation;

/**
Rounds a new image with a given corner size.

@param radius  The radius of each corner oval. Values larger than half the
               rectangle's width or height are clamped appropriately to half
               the width or height.
*/
- (nullable UIImage *)imageByRoundCornerRadius:(CGFloat)radius;

/**
Rounds a new image with a given corner size.

@param radius       The radius of each corner oval. Values larger than half the
                    rectangle's width or height are clamped appropriately to
                    half the width or height.
 
@param borderWidth  The inset border line width. Values larger than half the rectangle's
                    width or height are clamped appropriately to half the width
                    or height.
 
@param borderColor  The border stroke color. nil means clear color.
*/
- (nullable UIImage *)imageByRoundCornerRadius:(CGFloat)radius
                                   borderWidth:(CGFloat)borderWidth
                                   borderColor:(nullable UIColor *)borderColor;

/**
Rounds a new image with a given corner size.

@param radius       The radius of each corner oval. Values larger than half the
                    rectangle's width or height are clamped appropriately to
                    half the width or height.

@param corners      A bitmask value that identifies the corners that you want
                    rounded. You can use this parameter to round only a subset
                    of the corners of the rectangle.

@param borderWidth  The inset border line width. Values larger than half the rectangle's
                    width or height are clamped appropriately to half the width
                    or height.

@param borderColor  The border stroke color. nil means clear color.

@param borderLineJoin The border line join.
*/
- (nullable UIImage *)imageByRoundCornerRadius:(CGFloat)radius
                                       corners:(UIRectCorner)corners
                                   borderWidth:(CGFloat)borderWidth
                                   borderColor:(nullable UIColor *)borderColor
                                borderLineJoin:(CGLineJoin)borderLineJoin;

/**
 Returns a new image which is scaled from this image.
 The image will be stretched as needed.
 
 @param size  The new size to be scaled, values should be positive.
 
 @return      The new image with the given size.
 */
- (nullable UIImage *)imageByResizeToSize:(CGSize)size;

/**
 Returns a new image which is cropped from this image.
 
 @param rect  Image's inner rect.
 
 @return      The new image, or nil if an error occurs.
 */
- (nullable UIImage *)imageByCropToRect:(CGRect)rect;

#pragma mark - Image Effect

/**
 Returns a new image which is merged mask image
 
 @param maskImage  The mask image
 @param rect       The mask rect
 
 @return      The new image, or nil if an error occurs.
 */
- (nullable UIImage *)imageByMaskImage:(UIImage *)maskImage rect:(CGRect)rect;

/**
 Returns a new image which is append a image
 
 @param image     The appended image
 @param vertical  The direction of image
 */
- (UIImage *)imageWithAppendImage:(UIImage *)image vertical:(BOOL)vertical offset:(CGFloat)offset;

/**
 Returns a new image which is blured from this image
 
 @param blurRadius  The radius of the blur in points, 0 means no blur effect. Rang [0, 1]
 
 @return      The new image, or nil if an error occurs.
 */
//- (nullable UIImage *)imageByBlurRadius:(CGFloat)blurRadius;

#pragma mark - Compress

/**
 Compress the image to blow the byte and Returns the data
 
 @param maxByte Blow the byte after be compressed
 
 @return        The compressed data
 */
- (nullable NSData *)dataOfCompressedToMaxByte:(NSUInteger)maxByte;

/**
Compress the image to blow the byte and Returns a new image

@param maxByte Blow the byte after be compressed

@return        The new image, or nil if an error occurs.
*/
- (nullable UIImage *)imageByCompressedToMaxByte:(NSUInteger)maxByte;



// 根据图片控件以宽度为准缩放图片
- (nullable UIImage *)imageByResizeFitWidthToSize:(CGSize)size;

@end

NS_ASSUME_NONNULL_END
